package org.anq.ddm.controller;

import org.anq.ddm.pojo.Form;
import org.anq.ddm.service.FormService;
import org.anq.ddm.utils.ApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/form")
public class FormController {

    @Autowired
    FormService formService;

    /**
     *  获取所有表单
     * @return
     */
    @RequestMapping(value = "/forms", method = RequestMethod.GET)
    public ApiResult froms() {
        ApiResult result = new ApiResult();
        result.setData(this.formService.list());
        return result;
    }

    /**
     * 新建表单
     * @param
     * @return
     */
    @RequestMapping(value = "/form", method = RequestMethod.POST)
    public ApiResult addDdmForm(@RequestBody Form d){
        ApiResult result = new ApiResult();
        Form temp = this.formService.findByFromKey(d.getFormname());
        if(temp!=null) {
            d.setId(temp.getId());
            this.formService.updateById(d);
        }else {
            this.formService.save(d);
        }
        result.setMessage("表单增加成功");
        return result;
    }

    /**
     * 根据表单ID展示列表
     * @param formId
     * @return
     */
    @RequestMapping(value = "/form/{formId}", method = RequestMethod.GET)
    public ApiResult getFormById(@PathVariable String formId) {
        ApiResult result = new ApiResult();
        result.setData(this.formService.findByFromKey(formId).getFormvalue());
        return result;
    }

    /**
     * 表单列表
     * @return
     */
    @RequestMapping(value = "/formsEnabled", method = RequestMethod.GET)
    public ApiResult formsEnabled(){
        ApiResult result = new ApiResult();
        result.setData(this.formService.findByStatus(1));
        return result;
    }

}
